import 'package:flutter/material.dart';
import 'package:flutter_slidable/flutter_slidable.dart';
import 'package:tencent_cloud_chat_uikit/tencent_cloud_chat_uikit.dart';
import 'package:tencent_cloud_chat_uikit/ui/utils/time_ago.dart';
import 'package:xindong/common/xindong_define.dart';
import 'package:xindong/common/xindong_sezhi.dart';
import 'package:xindong/routes/tab_chats/controller/xindong_liaotian_vm.dart';
import 'package:xindong/routes/tab_chats/xindong_chat_message_type.dart';
import 'package:xindong/utils/xindong_text_util.dart';
import 'package:xindong/widgets/xindong_avatar.dart';
import 'package:xindong/widgets/xindong_count_red_dots.dart';

class XinDongChatConversationCell extends StatelessWidget {
  final XinDongLiaoTianClass model;
  final XinDongChatMessageType chatMessageType;
  final Function(V2TimConversation conversation) onTap; // 点击
  final Function(V2TimConversation conversation) onTapAvatar; // 点击头像
  final Function(V2TimConversation conversation) onLongPress; // 长按
  final Function(V2TimConversation conversation) onDelete; // 删除
  final Function(V2TimConversation conversation) onPin; // 置顶
  const XinDongChatConversationCell({
    Key? key,
    required this.model,
    required this.chatMessageType,
    required this.onTap,
    required this.onTapAvatar,
    required this.onLongPress,
    required this.onDelete,
    required this.onPin,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    if (TextUtil.isEmpty(model.conversation.userID)) {
      return const SizedBox();
    }
    if (model.conversation.type != ConversationType.V2TIM_C2C) {
      return const SizedBox();
    }
    return GestureDetector(
      onTap: () => onTap(model.conversation),
      onLongPress: () => onLongPress(model.conversation),
      behavior: HitTestBehavior.opaque,
      child: Slidable(
        endActionPane: ActionPane(
          extentRatio: 0.5,
          motion: const DrawerMotion(),
          children: _defaultSlideBuilder(model.conversation),
        ),
        child: _buildConversationItem(model.conversation),
      ),
    );
  }

  Widget _getTimeStringForChatWidget() {
    try {
      if (model.conversation.draftTimestamp != null && model.conversation.draftTimestamp != 0) {
        return Text(
          TimeAgo().getTimeStringForChat(model.conversation.draftTimestamp as int) ?? "",
          style: const TextStyle(fontSize: 13, color: DZColors.placeholderColor),
        );
      } else if (model.conversation.lastMessage != null) {
        return Text(
          TimeAgo().getTimeStringForChat(model.conversation.lastMessage!.timestamp as int) ?? "",
          style: const TextStyle(fontSize: 13, color: DZColors.placeholderColor),
        );
      }
    } catch (err) {}

    return Container();
  }

  Widget _buildConversationItem(V2TimConversation conversation) {
    return Container(
      color: conversation.isPinned == true ? DZColors.backgroundColor : DZColors.cellColor,
      padding: const EdgeInsets.only(left: kPaddingValue, right: kPaddingValue),
      margin: const EdgeInsets.only(bottom: 0.5),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          // 头像
          GestureDetector(
            onTap: () => onTapAvatar(conversation),
            behavior: HitTestBehavior.opaque,
            child: Stack(
              clipBehavior: Clip.none,
              children: [
                XinDongAvatar(url: '${conversation.faceUrl}', size: 55),
              ],
            ),
          ),
          const SizedBox(width: 10),
          Expanded(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    // 昵称
                    Expanded(
                      child: Text(
                        conversation.showName ?? conversation.userID ?? "",
                        maxLines: 1,
                        overflow: TextOverflow.ellipsis,
                        textAlign: TextAlign.left,
                        style: TextStyle(
                          color: (model.level ?? 0) > 0 ? Colors.red : DZColors.titleColor,
                          fontSize: 15,
                          fontWeight: FontWeight.w600,
                        ),
                      ),
                    ),

                    // 时间
                    _getTimeStringForChatWidget(),
                  ],
                ),
                const SizedBox(height: 5),
                SizedBox(
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                    children: [
                      // 类型
                      Expanded(
                        child: Text(
                          chatMessageType.getMessageType(conversation.lastMessage),
                          maxLines: 1,
                          overflow: TextOverflow.ellipsis,
                          textAlign: TextAlign.left,
                          style: const TextStyle(fontSize: 12, fontWeight: FontWeight.normal, color: DZColors.contentColor),
                        ),
                      ),
                      XinDongUnreadCountWidget(count: conversation.unreadCount, height: 16, fontSize: 12),
                    ],
                  ),
                )
              ],
            ),
          ),
        ],
      ),
    );
  }

  List<ConversationItemSlidePanel> _defaultSlideBuilder(V2TimConversation conversation) {
    return [
      ConversationItemSlidePanel(
        onPressed: (context) => onDelete(conversation),
        backgroundColor: Colors.red,
        foregroundColor: Colors.white,
        label: "删除",
        spacing: 0,
        autoClose: true,
      ),
      ConversationItemSlidePanel(
        onPressed: (context) => onPin(conversation),
        backgroundColor: Colors.orange,
        foregroundColor: Colors.white,
        label: conversation.isPinned! ? ("取消\n置顶") : ("置顶"),
      ),
    ];
  }
}
